<?php
/******************************************************
v4 - Management Framework
Copyright (C) 2008  Gary Taylor, gMerc Incorporation
http://www.gmerc.com/v4/

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

gary@gmerc.com
******************************************************/
class report {
  function loaddata() {
    global $masterclass;

    $masterclass->loadmodule('invoices');

    $masterclass->site['postfields'][] = array(
      'trackingid'=>'report'
    );

    $masterclass->websiteconfig['javascript_include'][]='include/date-picker.js';

    //if (!isset($masterclass->sitevars['invoices_reportdate_start'])) { $masterclass->sitevars['invoices_reportdate_start']=date("Y/m/d",mktime(date("H"),date("i"),date("s"),date("m")-1,1,date("Y"))); }
    //if (!isset($masterclass->sitevars['invoices_reportdate_end'])) { $masterclass->sitevars['invoices_reportdate_end']=date("Y/m/d",mktime(date("H"),date("i"),date("s"),date("m")-1,date("t",mktime(date("H"),date("i"),date("s"),date("m")-1,1,date("Y"))),date("Y"))); }
  return;
  }

  function display() {
  global $masterclass;

?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top">
    <td>
<?php

  $nav='';
  $nav.='<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr valign="middle" class="sitebox_navigation">';
  $querystring=$masterclass->trackingvar_query();
  $nav.='<td>';
  $nav.='Date Range:&nbsp;'.$masterclass->allclasses['websites']->disp_inputtext('invoices_reportdate_start','','11','10').'&nbsp;<img OnClick="show_calendar(\'frm.invoices_reportdate_start\');" style="cursor:pointer;" align="absmiddle" src="'.$masterclass->mainconfig['dir'].'art/calendar.png" border="0"> To '.$masterclass->allclasses['websites']->disp_inputtext('invoices_reportdate_end','','11','10').'&nbsp;<img OnClick="show_calendar(\'frm.invoices_reportdate_end\');" style="cursor:pointer;" align="absmiddle" src="'.$masterclass->mainconfig['dir'].'art/calendar.png" border="0">&nbsp;<input type="button" class="button" OnClick="window.location=\'report.php?invoices_reportdate_start=\'+document.getElementById(\'invoices_reportdate_start\').value+\'&invoices_reportdate_end=\'+document.getElementById(\'invoices_reportdate_end\').value+\'&report=\'+document.getElementById(\'report\').value;" value=" Search ">';
  $nav.='&nbsp;</td>';
  $nav.='<td align="right"><input type="button" class="button" value="Close" OnClick="closewindow();"></td>';
  $nav.='</tr></table>';
  echo $masterclass->allclasses['websites']->sitebox_navigation($nav);
?>
  </td>
  </tr>
</table>
<table width="100%" cellpadding="2" cellspacing="5" border="0">
  <tr valign="top">
  <td width="100%">
<?php
echo '<a href="#" OnClick="window.location=\'report.php?invoices_reportdate_start=\'+document.getElementById(\'invoices_reportdate_start\').value+\'&invoices_reportdate_end=\'+document.getElementById(\'invoices_reportdate_end\').value+\'&report=totals\';" class="sitebox_content">Totals Report</a><br>';
echo '<a href="#" OnClick="window.location=\'report.php?invoices_reportdate_start=\'+document.getElementById(\'invoices_reportdate_start\').value+\'&invoices_reportdate_end=\'+document.getElementById(\'invoices_reportdate_end\').value+\'&report=customers\';" class="sitebox_content">Customers Report</a><br>';
echo '<a href="#" OnClick="window.location=\'report.php?invoices_reportdate_start=\'+document.getElementById(\'invoices_reportdate_start\').value+\'&invoices_reportdate_end=\'+document.getElementById(\'invoices_reportdate_end\').value+\'&report=payments\';" class="sitebox_content">Payment Report</a><br>';
echo '<br>';

    $strwhere='';
    if (isset($masterclass->sitevars['invoices_reportdate_start']) && $masterclass->sitevars['invoices_reportdate_start']!='') {
      if ($strwhere!='') { $strwhere.=' AND '; }
      $strwhere.=sprintf(" (`documentdate`>='%s') ",$masterclass->allclasses['mysql_functions']->formatmysqlstr($masterclass->sitevars['invoices_reportdate_start']));
    }
    if (isset($masterclass->sitevars['invoices_reportdate_end']) && $masterclass->sitevars['invoices_reportdate_end']!='') {
      if ($strwhere!='') { $strwhere.=' AND '; }
      $strwhere.=sprintf(" (`documentdate`<='%s') ",$masterclass->allclasses['mysql_functions']->formatmysqlstr($masterclass->sitevars['invoices_reportdate_end']));
    }

    switch ($masterclass->sitevars['report']) {
     default:
     case 'totals':

$invoicecnt=$masterclass->fetch_recordcount('invoices_documents',$strwhere);
$listarray=$masterclass->fetch_records('invoices_documents',0,$strwhere);

$reportqty=0;
$reportsubtotal=0;
$reporttax=0;
$reporttotal=0;
if (!empty($listarray)) {
foreach ($listarray as $invoice) {
  $invoiceitems=$masterclass->allclasses['invoices']->fetch_invoices_items($invoice['id']);
  $invoicetotals=$masterclass->allclasses['invoices']->calc_invoicetotal($invoiceitems);

  $reportqty+=$invoicetotals['qty'];
  $reportsubtotal+=$invoicetotals['subtotal'];
  $reporttax+=$invoicetotals['tax'];
  $reporttotal+=$invoicetotals['total'];
}
}
      echo '<table width="100%" cellpadding="3" cellspacing="0" border="0" class="sitebox_content">';
      echo '<tr class="sitebox_navigation"><td><b>Invoices</b></td><td><b>Items</b></td><td><b>Tax</b></td><td><b>Subtotal</b></td><td><b>Total</b></td></tr>';
      echo '<tr><td style="border-bottom:1px solid;">'.$invoicecnt.'</td><td style="border-bottom:1px solid;">'.$reportqty.'</td><td style="border-bottom:1px solid;">$'.number_format($reporttax,2).'</td><td style="border-bottom:1px solid;">$'.number_format($reportsubtotal,2).'</td><td style="border-bottom:1px solid;">$'.number_format($reporttotal,2).'</td></tr>';
      echo '</table>';
     break;
     case 'payments':
      if ($strwhere!='') { $strwhere.=' AND '; }
      $strwhere.=" (`paid`!='0000-00-00') ";

      $invoices=$masterclass->fetch_records('invoices_documents',0,$strwhere,' `paid` ');

      $invoiceitems=$masterclass->allclasses['invoices']->fetch_invoices_items($invoice['id']);
      $invoicetotals=$masterclass->allclasses['invoices']->calc_invoicetotal($invoiceitems);
      $totalreportinvoicecnt=0;
      echo '<table width="100%" cellpadding="3" cellspacing="0" border="0" class="sitebox_content">';
      echo '<tr class="sitebox_navigation"><td><b>Paid Date</b></td><td><b>Name</b></td><td><b>Tax</b></td><td><b>Subtotal</b></td><td><b>Total</b></td></tr>';
      if (!empty($invoices)) {
      foreach ($invoices as $invoice) {
        $invoiceitems=$masterclass->allclasses['invoices']->fetch_invoices_items($invoice['id']);
        $invoicetotals=$masterclass->allclasses['invoices']->calc_invoicetotal($invoiceitems);
        echo '<tr OnMouseOver="this.className=\'sitebox_content_mouseover\';" OnMouseOut="this.className=\'sitebox_content\';"><td style="border-bottom:1px solid;">'.$invoice['paid'].'</td><td style="border-bottom:1px solid;">'.$invoice['recipientlineone'].'</td><td style="border-bottom:1px solid;">$'.number_format($invoicetotals['tax'],2).'</td><td style="border-bottom:1px solid;">$'.number_format($invoicetotals['subtotal'],2).'</td><td style="border-bottom:1px solid;">$'.number_format($invoicetotals['total'],2).'</td></tr>';
        $reportqty+=$invoicetotals['qty'];
        $reportsubtotal+=$invoicetotals['subtotal'];
        $reporttax+=$invoicetotals['tax'];
        $reporttotal+=$invoicetotals['total'];

        $totalreportinvoicecnt+=1;
      }
      }
      echo '<tr class="sitebox_navigation"><td style="border-top:1px solid;"><b>Totals</b></td><td style="border-top:1px solid;"><b>'.$totalreportinvoicecnt.' Payments</b></td><td style="border-top:1px solid;"><b>$'.number_format($reporttax,2).'</b></td><td style="border-top:1px solid;"><b>$'.number_format($reportsubtotal,2).'</b></td><td style="border-top:1px solid;"><b>$'.number_format($reporttotal,2).'</b></td></tr>';
      echo '</table>';
     break;
     case 'customers':
      $masterclass->loadmodule('customers');
      $customers=$masterclass->fetch_records('customers',0);

      $totalreportinvoices=0;
      $totalreportqty=0;
      $totalreportsubtotal=0;
      $totalreporttax=0;
      $totalreporttotal=0;
      echo '<table width="100%" cellpadding="3" cellspacing="0" border="0" class="sitebox_content">';
      echo '<tr class="sitebox_navigation"><td><b>Customer</b></td><td><b>Invoices</b></td><td><b>Items</b></td><td><b>Tax</b></td><td><b>Subtotal</b></td><td><b>Total</b></td></tr>';


      if (!empty($customers)) {
      foreach ($customers as $customer) {
        $straddwhere=sprintf(" (`customer`=%d) ",$masterclass->allclasses['mysql_functions']->formatmysqldbl($customer['id']));

        if ($strwhere!='') { $straddwhere.=' AND '.$strwhere; }
        $invoicecnt=$masterclass->fetch_recordcount('invoices_documents',$straddwhere);
        $invoices=$masterclass->fetch_records('invoices_documents',0,$straddwhere);

        $reportqty=0;
        $reportsubtotal=0;
        $reporttax=0;
        $reporttotal=0;

        if (!empty($invoices)) {
        foreach ($invoices as $invoice) {
          $invoiceitems=$masterclass->allclasses['invoices']->fetch_invoices_items($invoice['id']);
          $invoicetotals=$masterclass->allclasses['invoices']->calc_invoicetotal($invoiceitems);

          $reportqty+=$invoicetotals['qty'];
          $reportsubtotal+=$invoicetotals['subtotal'];
          $reporttax+=$invoicetotals['tax'];
          $reporttotal+=$invoicetotals['total'];

          $totalreportinvoices+=1;
        }
        }

        $totalreportqty+=$reportqty;
        $totalreportsubtotal+=$reportsubtotal;
        $totalreporttax+=$reporttax;
        $totalreporttotal+=$reporttotal;
        echo '<tr OnMouseOver="this.className=\'sitebox_content_mouseover\';" OnMouseOut="this.className=\'sitebox_content\';"><td style="border-bottom:1px solid;">'.$masterclass->allclasses['customers']->fetch_customername($customer['id']).'</td><td style="border-bottom:1px solid;">'.$invoicecnt.'</td><td style="border-bottom:1px solid;">'.$reportqty.'</td><td style="border-bottom:1px solid;">$'.number_format($reporttax,2).'</td><td style="border-bottom:1px solid;">$'.number_format($reportsubtotal,2).'</td><td style="border-bottom:1px solid;">$'.number_format($reporttotal,2).'</td></tr>';

      }
      }

        $straddwhere=" ((`customer` NOT IN (SELECT `id` FROM `customers`))) ";

        if ($strwhere!='') { $straddwhere.=' AND '.$strwhere; }
        $invoicecnt=$masterclass->fetch_recordcount('invoices_documents',$straddwhere);
        $invoices=$masterclass->fetch_records('invoices_documents',0,$straddwhere);

        $reportqty=0;
        $reportsubtotal=0;
        $reporttax=0;
        $reporttotal=0;
        if (!empty($invoices)) {
        foreach ($invoices as $invoice) {
          $invoiceitems=$masterclass->allclasses['invoices']->fetch_invoices_items($invoice['id']);
          $invoicetotals=$masterclass->allclasses['invoices']->calc_invoicetotal($invoiceitems);

          $reportqty+=$invoicetotals['qty'];
          $reportsubtotal+=$invoicetotals['subtotal'];
          $reporttax+=$invoicetotals['tax'];
          $reporttotal+=$invoicetotals['total'];

          $totalreportinvoices+=1;
        }
        }

        $totalreportqty+=$reportqty;
        $totalreportsubtotal+=$reportsubtotal;
        $totalreporttax+=$reporttax;
        $totalreporttotal+=$reporttotal;
        echo '<tr OnMouseOver="this.className=\'sitebox_content_mouseover\';" OnMouseOut="this.className=\'sitebox_content\';"><td style="border-bottom:1px solid;">Other</td><td style="border-bottom:1px solid;">'.$invoicecnt.'</td><td style="border-bottom:1px solid;">'.$reportqty.'</td><td style="border-bottom:1px solid;">$'.number_format($reporttax,2).'</td><td style="border-bottom:1px solid;">$'.number_format($reportsubtotal,2).'</td><td style="border-bottom:1px solid;">$'.number_format($reporttotal,2).'</td></tr>';


      echo '<tr class="sitebox_navigation"><td style="border-bottom:1px solid;"><b>Totals</b>;</td><td style="border-bottom:1px solid;"><b>'.$totalreportinvoices.'</b></td><td style="border-bottom:1px solid;"><b>'.$totalreportqty.'</b></td><td style="border-bottom:1px solid;"><b>$'.number_format($totalreporttax,2).'</b></td><td style="border-bottom:1px solid;"><b>$'.number_format($totalreportsubtotal,2).'</b></td><td style="border-bottom:1px solid;"><b>$'.number_format($totalreporttotal,2).'</b></td></tr>';
      echo '</table>';
    break;
    }
?>
</td></tr></table>
<?php

  return;
  }
}
?>